<template>
	<div class="wrapper">

		<!-- header部分 -->
		<header>
			<p>修改食物信息</p>
		</header>

		<!-- 表单部分 -->
		<ul class="form-box">
			<li>
				<div class="title">
					食物名称：
				</div>
				<div class="content">
					<input type="text" v-model="food.foodName">
				</div>
			</li>
			<li>
				<div class="title">
					食物说明：
				</div>
				<div class="content">
					<input type="text" v-model="food.foodExplain">
				</div>
			</li>
			<li>
				<div class="title">
					食物图片：
				</div>
				<div class="content">
					<input type="text" v-model="food.foodImg">
				</div>
			</li>
			<li>
				<div class="title">
					食物价格：
				</div>
				<div class="content">
					<input type="text" v-model="food.foodPrice">
				</div>
			</li>
		</ul>


		<div class="button-update">
			<button @click="update()">更新</button>
		</div>

		<!-- 底部菜单部分 -->
		<Footer></Footer>
	</div>
</template>

<script setup>
import { ref } from 'vue';
import Footer from '../components/Footer.vue';
import axios from 'axios';
import qs from 'qs';
import { useRouter, useRoute } from 'vue-router';

const food = ref({});
const router = useRouter();
const route = useRoute();

// 获取路由参数中的food信息，并将其转换为响应式数据

	food.value = JSON.parse(route.query.food);

const update = () => {
  if (food.value.foodName == '') {
    alert('食物名称不能为空！');
    return;
  }
  if (food.value.foodExplain == '') {
    alert('食物说明不能为空！');
    return;
  }
  if (food.value.foodImg == '') {
    alert('食物图片不能为空！');
    return;
  }
  if (food.value.foodPrice == '') {
    alert('食物价格不能为空！');
    return;
  }

  // 更新请求
  axios.post('FoodController/updateFoodInfoById', qs.stringify(food.value))
    .then(response => {
      if (response.data > 0) {
        alert('更新食物成功！');
        router.go(-1);
      } else {
        alert('更新食物失败！');
      }
    })
    .catch(error => {
      console.error(error);
    });
};
</script>

<style scoped>
	/****************** 总容器 ******************/
	.wrapper {
		width: 100%;
		height: 100%;
	}

	/****************** header部分 ******************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;

		display: flex;
		justify-content: center;
		align-items: center;
	}

	/****************** 表单部分 ******************/
	.wrapper .form-box {
		width: 100%;
		margin-top: 12vw;
	}

	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}

	.wrapper .form-box li .title {
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}

	.wrapper .form-box li .content {
		flex: 1;
	}

	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}

	.wrapper .button-update {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-update button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #fff;
		background-color: #38CA73;
		border-radius: 4px;

		border: none;
		outline: none;
	}

	.wrapper .button-register {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-register button {
		width: 100%;
		height: 10vw;
		font-size: 3.8vw;
		font-weight: 700;
		color: #666;
		background-color: #EEE;
		border-radius: 4px;

		border: none;
		outline: none;
		border: solid 1px #DDD;
	}
</style>
